//
//  PBMyContractController.m
//  PBHomeDemo
//
//  Created by Playboy on 2018/3/20.
//  Copyright © 2018年 tmkj. All rights reserved.
//

#import "PBMyContractController.h"
#import "PBMyContractCell.h"
#import "PBMyContractModel.h"
#import "TMEmptyView.h"

static NSString * const kPBMyContractCell = @"PBMyContractCell";

@interface PBMyContractController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray   *dataSource;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) TMEmptyView *emptyView;
@end

@implementation PBMyContractController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configure];
}
- (void)configure {
    [self configureView];
    [self configureData];
}

- (void)configureView {
    [self.tableView registerNib:[UINib nibWithNibName:kPBMyContractCell bundle:nil] forCellReuseIdentifier:kPBMyContractCell];
    [TMRefreshHelper headerRefresh:self.tableView withTarget:self action:@selector(configureData)];
    [TMRefreshHelper footerRefresh:self.tableView withTarget:self action:@selector(loadMoreMyContractData)];
}
- (void)configureData {
    self.page = 1;
    NSDictionary *param = @{@"currentPage":@"1",@"pageSize":@"10"};
    [self showHud];
    [TMNetwork requestGetWithParam:param url:@"/user/contract/list" callback:^(TMBaseReqModel *model) {
        [self hideHud];
        [self.tableView.mj_header endRefreshing];
        if (model.code == 200) {
            self.dataSource = [PBMyContractModel objectArrayWithKeyValuesArray:model.data];
            if (self.dataSource.count < 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
            if (self.dataSource.count == 0) {
                [self.view addSubview:self.emptyView];
            }else {
                [self.emptyView removeFromSuperview];
            }
            [self.tableView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
}

- (void)loadMoreMyContractData {
    self.page += 1;
    NSDictionary *param = @{@"currentPage":[NSString stringWithFormat:@"%zd",self.page],@"pageSize":@"10"};
    [TMNetwork requestGetWithParam:param url:@"/user/contract/list" callback:^(TMBaseReqModel *model) {
        [self.tableView.mj_header endRefreshing];
        if (model.code == 200) {
            NSArray *array = [PBMyContractModel objectArrayWithKeyValuesArray:model.data];
            if (array.count < 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
            [self.dataSource addObjectsFromArray:array];
            [self.tableView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
}


#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PBMyContractCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBMyContractCell forIndexPath:indexPath];
    [cell refreshWithModel:self.dataSource[indexPath.section]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 15;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 212;
}
- (TMEmptyView *)emptyView {
    if (!_emptyView) {
        _emptyView = [TMEmptyView TMEmptyView];
        _emptyView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    }
    return _emptyView;
}

#pragma mark - super
- (void)resetNavigationBarItems {
    [super resetNavigationBarItems];
    self.title = @"我的合同";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
